package com.dzl.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * @Project Name: yomeApi
 * @File Name: StringUtil.java
 * @Package Name：com.yome.utils
 * @Description: 自定义工具类
 * @Author：lujianmin ljm_3331@126.com
 * @Date：2015年8月8日 下午4:18:16
 * @Copyright 2015, 武汉由米定制科技有限公司 All rights reserved.
 * @version V1.0
 */
public class StringUtil {

	/**
	 * getRandomString:生成指定长度的随机字符串
	 * @param length
	 * @return
	 */
	public static String getRandomStringHaveNumberAndEnglish(int length) { // length表示生成字符串的长度
		String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 生成指定长度的随机字符串，包含大写字母和数字
	 * @date 2021/7/17 15:58
	 * @author caobucao
	 * @param length 表示生成字符串的长度
	 * @return java.lang.String
	 */
	public static String getRandomStringHaveNumberAndEnglishUpper(int length) { // length表示生成字符串的长度
		String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 生成指定长度的随机字符串，包含大写字母和数字，去掉IO
	 * @date 2021/7/17 15:58
	 * @author caobucao
	 * @param length 表示生成字符串的长度
	 * @return java.lang.String
	 */
	public static String getRandomStringHaveNumberAndEnglishUpperRemoveIO10(int length) {
		String base = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * getRandomString:生成指定长度的随机字符串
	 *
	 * @param length
	 * @return
	 */
	public static String getRandomStringHasOnlyNumber(int length) { // length表示生成字符串的长度
		String base = "0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * generateOrderNo:生成订单编号
	 *
	 * @return
	 */
	public static String generateOrderNo() {
		SimpleDateFormat df = new SimpleDateFormat("MMddHHmmssSSS");
		String id = df.format(new Date()) + new Random().nextInt(100);
		return id;
	}
	/**
	 * 生成账单编号
	 * @return
	 */
	public static String getBillNumber() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String id = df.format(new Date()) + new Random().nextInt(100);
		return id;
	}

	/**
	 * 生成发票编号
	 * @return
	 */
	public static String getInvoiceNo() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		return "FP"+df.format(new Date()) + getRandomStringHasOnlyNumber(4);
	}

	public static String generatePaymentNo() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String id = "DK"+df.format(new Date()) + new Random().nextInt(100);
		return id;
	}

	/**
	 * 生成批次号
	 * @return
	 */
	public static String getBatchNumber(String prefix) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String id = df.format(new Date()) + getFourRandomString(3);
		return prefix+id;
	}

	/**
	 * divide:整除相除得到百分比
	 *
	 * @param a
	 * @param b
	 * @return
	 */
	public static String divide(int a, int b) {
		if (b > 0) {
			float num = (float) a / b;
			DecimalFormat df = new DecimalFormat("0%");// 格式化小数
			return df.format(num);
		} else {
			return "0%";
		}
	}

	/**
	 * 获取远程IP地址
	 *
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 去除数组中重复的记录
	 *
	 * @param arr
	 * @return
	 */
	public static Integer[] arrayUnique(Integer[] arr) {
		// array_unique
		List<Integer> list = new LinkedList<Integer>();
		for (int i = 0; i < arr.length; i++) {
			if (!list.contains(arr[i])) {
				list.add(arr[i]);
			}
		}
		return (Integer[]) list.toArray(new Integer[list.size()]);
	}

	public static boolean isMobile(String mobiles) {
		Pattern p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$");
		Matcher m = p.matcher(mobiles);
		System.out.println(m.matches() + "---");
		return m.matches();
	}

	/**
	 * @param 过滤所有的特殊字符  只允许数字、字母和汉字
	 * @return
	 * @throws PatternSyntaxException
	 */
	public static  String StringFilterSpecialCharacter(String str){
		  // 只允许字母和数字
		  // String regEx = "[^a-zA-Z0-9-,]";
		  // 清除掉所有特殊字符
		  String regEx="[`~!@#$%^&*+=|{}':,;'//[//].<>/?~！@#￥%……&*——+|{}【】‘；：”“’。，、？_]";
		  Pattern p = Pattern.compile(regEx);
		  Matcher m = p.matcher(str);
		  return m.replaceAll("").trim().toString();
   }
	/**
	 * 校验字符串长度是否符合 一个中文汉字占2个字符
	 * @version 定制链5.3.2原型需求
	 * @date 2020/9/30 11:39
	 * @author lishunxing
	 * @param str 校验的字符串
 	 * @param i 合法长度
	 * @return
	 */
	public static boolean checkChineseLength(String str, int i) {
		if(StringUtils.isBlank(str)){
			return false;
		}
		String regEx = "[\u4e00-\u9fa5]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll("aa").length() <= i;
	}

	/**
	 * 描述: 根据长度生成随机数
	 * @date 2022/2/15 9:39
	 * @author wangchao
	 * @Param
	 * @return
	**/
	public static String getFourRandomString(int length) { // length表示生成字符串的长度
		String base = "0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		String a = "1234567890".substring(0,8);
		System.out.println(a);

	}

}
